﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;
using BLL;

namespace App.Controllers
{
    public class VehicleQueryServiceController : BaseController
    {
        //车辆使用查询
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List()
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            List<vi_UseVehicle> list = null;
            //默认选择最后一个死者
            var dead = dbe.vi_UseVehicle.OrderByDescending(m => m.DeadNO).FirstOrDefault();
            if (dead == null)
            {
                return Json(new List<vi_UseVehicle>(), JsonRequestBehavior.AllowGet);
            }
            var res = dbe.vi_UseVehicle.Where(m => m.DeadNO == dead.DeadNO);
            list = res.OrderBy(m => m.DeadNO).ToList();
            decimal? fees = res.Sum(m => m.ServiceFee);
            int? count = res.Sum(m => m.ServiceCount);
            var foot = new vi_UseVehicle
            {
                ServiceCount = count,
                ServiceFee = fees,
                UseVehicleNO = "合计："
            };
            return Json(
                new
                {
                    success = true,
                    rows = list,
                    footer = new object[] { foot }
                }, JsonRequestBehavior.AllowGet);
        }

        //根据时间段和车辆或司机查询
        public ActionResult Search(FormCollection collection)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            string driverNO = collection["DriverNO"].Trim();
            string vehicleNO = collection["VehicleNO"].Trim();
            string starttime = collection["StartTime"];
            string endtime = collection["EndTime"];
            DateTime st = DateTime.MinValue;
            DateTime et = DateTime.MaxValue;
            try
            {
                st = DateTime.Parse(starttime);
            }
            catch
            {
                st = DateTime.MinValue;
            }
            try
            {
                et = DateTime.Parse(endtime);
                et = et.AddDays(1);
            }
            catch
            {
                et = DateTime.MaxValue;
            }

            var res = dbe.vi_UseVehicle.Where(m => (m.DepartureTime == null || (st <= m.DepartureTime) && et >= m.DepartureTime));
            if (!string.IsNullOrWhiteSpace(driverNO) && !string.IsNullOrWhiteSpace(vehicleNO))
            {
                res = res.Where(m => (m.Name.Contains(driverNO) && m.VehicleNO.Contains(vehicleNO))).OrderBy(m => m.Name);
            }
            else if (!string.IsNullOrWhiteSpace(driverNO))
            {
                res = res.Where(m => m.Name.Contains(driverNO)).OrderBy(m => m.Name);
            }
            else if (!string.IsNullOrWhiteSpace(vehicleNO))
            {
                res = res.Where(m => m.VehicleNO.Contains(vehicleNO)).OrderBy(m => m.VehicleNO);
            }
            var list = res.ToList();
            decimal? fees = res.Sum(m => m.ServiceFee);
            int? count = res.Sum(m => m.ServiceCount);
            var foot = new vi_UseVehicle
            {
                ServiceCount = count,
                ServiceFee = fees,
                UseVehicleNO = "合计："
            };
            return Json(
                new
                {
                    success = true,
                    rows = list,
                    footer = new object[] { foot }
                }, JsonRequestBehavior.AllowGet);
        }

        //打印查询到的车辆使用情况
        public ActionResult PrintUseVehicleList()
        {
            return View();
        }

    }
}
